package com.nh.orderAction;

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.nh.orderEntity.VegetablesEntity;
import com.opensymphony.xwork2.ActionContext;

public class FileDownloadAction extends OrderBaseAction {
	private String fileName;// 要下载的文件名
	private String code;

	public String execute() throws Exception {
		return SUCCESS;
	}

	// 下载文件
	public InputStream getDownloadFile() {
		InputStream is = null;
		try {
			
			List<VegetablesEntity> vegetablesEntityList = this.getBaseService().findByProperty("code", code, VegetablesEntity.class);
			if (vegetablesEntityList.size() > 0) {
				VegetablesEntity vegetablesEntity = vegetablesEntityList.get(0);
				if (null != vegetablesEntity.getImg()) {
					//Blob photo = vegetablesEntity.getImage();
					is = new ByteArrayInputStream(vegetablesEntity.getImg());
				}
			}
			// is = new FileInputStream("D:/" + fileName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(null==is){
			try {
				is = new FileInputStream(getSavePath() + "/default_image.jpg");
				//is = new FileInputStream(getSavePath() + "/Tulips.jpg");
				
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return is;
	}

	public void setFileName(String fileName) {
		try {// 解决中文文件名问题
			this.fileName = new String(fileName.getBytes("ISO-8859-1"), "GBK");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	public String getFileName() {
		String name = "";
		try {// 解决下载文件中文文件名问题
			name = new String(fileName.getBytes("GBK"), "ISO8859-1");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return name;
	}

	/**
	 * @return the code
	 */
	public String getCode() {
		return code;
	}

	/**
	 * @param code the code to set
	 */
	public void setCode(String code) {
		this.code = code;
	}
}